<?php
namespace App\Http\Controllers;
use App\Exceptions\ValidateException;
use App\Repository\Contracts\TaoCangInterface;
use App\Repository\Contracts\GoodsInterface;
use App\Repository\Contracts\ShoppingCartInterface;
use App\Support\Helper\CommonHelper;
use Illuminate\Http\Request;
use Auth;

/**
 * @Author:: HuangYin
 * @DateTime: 2017-01-17 20:19
 *
 * @Description: 购物车
 */
class ShoppingCartController extends Controller
{
    /**
     * @var Request
     */
    protected $request;

    /**
     * @var ShoppingCartInterface
     */
    protected $shoppingCart;

    protected $uid;

    /**
     * ShoppingCartController constructor.
     * @param Request $request
     * @param ShoppingCartInterface $shoppingCart
     */
    public function __construct(Request $request, ShoppingCartInterface $shoppingCart, TaoCangInterface $taoCang,GoodsInterface $goods)
    {
        $this->request = $request;

        $this->shoppingCart = $shoppingCart;

        $this->taocang = $taoCang;

        $this->goods=$goods;

        $this->uid = CommonHelper::getToken(Auth::user())['uid'];
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function index()
    {

        $uid = $this->request->input('memberId', null);
        $map = $this->searchMap([
            ['uid', $uid, '='],
            ['admin_id', $this->uid, '='],
            ['status', 1, '=']
        ]);
        $list = $this->shoppingCart->lists($map);

        return $this->success($list);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function created()
    {
        try {
            $data['goods_id'] = $this->request->input('goods_id');
            $data['uid'] = $this->request->input('member_id', null)?:null;
            $data['admin_id'] = $this->uid;
            $id = $this->shoppingCart->created($data);
            return $this->success($id);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function updateCount($id)
    {
        $counts = $this->request->input('counts');
        try {
            $id = $this->shoppingCart->updateCount($id, $counts);
            return $this->success($id);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function pending()
    {
        try {
            $result = $this->shoppingCart->pending($this->uid);
            $result['total'] = count($result);

            return $this->success($result);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }

    public function pendingDetail()
    {
        $pendingNo = $this->request->input('pending_no', 0);
        $map = $this->searchMap([
            ['pending_no', $pendingNo, '='],
            ['admin_id', $this->uid, '=']
        ]);

        $list = $this->shoppingCart->lists($map);

        return $this->success($list);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function createdPending()
    {
        try {
            $result = $this->shoppingCart->createdPending($this->uid);

            return $this->success($result);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function cancelPending()
    {
        $pendingNo = $this->request->input('pendingNo', 0);
        try {
            $result = $this->shoppingCart->cancelPending($this->uid, $pendingNo);

            return $this->success($result);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function getPending()
    {
        $pendingNo = $this->request->input('pendingNo', 0);
        try {
            $result = $this->shoppingCart->getPending($this->uid, $pendingNo);

            return $this->success($result);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function deleted()
    {
        $ids = $this->request->input('ids');
        try {
            $result = $this->shoppingCart->deleted($ids);

            return $this->success($result);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }

    /**
     * @return \Illuminate\Http\JsonResponse|mixed
     */
    public function clear()
    {
        try {
            $result = $this->shoppingCart->clear($this->uid);

            return $this->success($result);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }
}